package com.example.certificate.controller;


import com.aliyuncs.exceptions.ClientException;
import com.example.certificate.annoatiton.CurrentUser;
import com.example.certificate.annoatiton.LoginToken;
import com.example.certificate.annoatiton.PassToken;
import com.example.certificate.dto.*;
import com.example.certificate.dto.common.DelByIdDto;
import com.example.certificate.dto.common.UserBasePageDto;
import com.example.certificate.entity.OrgUser;
import com.example.certificate.entity.User;
import com.example.certificate.execption.BusinessException;
import com.example.certificate.service.OrgUserService;
import com.example.certificate.service.UserService;
import com.example.certificate.utils.ResultEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * <p>
 * 用户登陆信息数据表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/domain/user")
@Api(tags = {"用户账号登录设置"}, value = "/domain/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrgUserService orgUserService;


    @ApiOperation(value = "用户登录（注：用户名、密码登录）")
    @PassToken
    @PostMapping(value = "/login")
    public ResultEntity login(@RequestBody LoginDTO dto) throws BusinessException {
        return new ResultEntity(userService.login(dto));
    }


    @ApiOperation(value = "发送手机验证码")
    @PassToken
    @PostMapping(value = "/sendSms")
    public ResultEntity sendSms(@Validated(value = {SendSmsDTO.sendGroup.class}) @RequestBody SendSmsDTO dto) throws BusinessException, ClientException {
        String s = userService.sendSms(dto);
        return new ResultEntity(s);
    }


    @ApiOperation(value = "更新用户登录密码")
    @PassToken
    @PostMapping(value = "/updatePass")
    public ResultEntity updatePass(@Validated(value = {UpdatePassDto.updateData.class}) @RequestBody UpdatePassDto dto) throws BusinessException, ClientException {
        userService.updatePass(dto);
        return new ResultEntity();
    }


    @ApiOperation(value = "用户登陆注册功能")
    @PassToken
    @PostMapping(value = "/registerInfo")
    public ResultEntity registerInfo(@Validated(value = {RegisterDto.insUser.class}) @RequestBody RegisterDto dto) throws BusinessException {
        userService.registerInfo(dto);
        return new ResultEntity();
    }


    @ApiOperation(value = "完善用户信息")
    @PassToken
    @PostMapping(value = "/perfectData")
    public ResultEntity perfectData(@Validated(value = {PerfectInfoDto.perfect.class}) @RequestBody PerfectInfoDto dto) {
        userService.perfectData(dto);
        return new ResultEntity();
    }

    @ApiOperation(value = "分页查询用户数据信息")
    @LoginToken
    @PostMapping(value = "/selBasePage")
    public ResultEntity selBasePage(@RequestBody UserBasePageDto dto, @Ignore @CurrentUser User user){
        return new ResultEntity(orgUserService.selBasePage(dto));
    }


    @ApiOperation(value = "后台管理员数据查询")
    @LoginToken
    @PostMapping(value = "/selectManagement")
    public ResultEntity selectManagement(@RequestBody UserBasePageDto dto, @Ignore @CurrentUser User user){
        return new ResultEntity(orgUserService.selectManagement(dto));
    }


    @ApiOperation(value = "根据id删除用户数据")
    @LoginToken
    @PostMapping(value = "/delByUserId")
    public ResultEntity delByUserId(@Validated(value = {DelByIdDto.delDataById.class}) @RequestBody DelByIdDto dto){
        return userService.delByUserId(dto);
    }

    @ApiOperation(value = "查询用户基本信息")
    @LoginToken
    @PostMapping(value = "/selBaseInfo")
    public ResultEntity selBaseInfo(@Ignore @CurrentUser User user){
        return userService.selBaseInfo(user.getId());
    }


    @ApiOperation(value = "修改用户信息")
    @LoginToken
    @PostMapping(value = "/updateUserInfo")
    public ResultEntity updateUserInfo(@RequestBody OrgUser dto){
        return userService.updateUserInfo(dto);
    }

    @ApiOperation(value = "修改用户权限")
    @LoginToken
    @PostMapping(value = "/updateUserRole")
    public ResultEntity updateUserRole(@RequestBody ManagementDto dto){
        return userService.updateUserRole(dto);
    }


    @ApiOperation(value = "新增用户注册事件 app")
    @PostMapping(value = "/insertUser")
    public ResultEntity insertUser(@RequestBody InsertRegisterDto dto) throws BusinessException {
        return userService.insertUser(dto);
    }

    @LoginToken
    @ApiOperation("修改数据状态")
    @PostMapping(value = "/editStatus")
    public ResultEntity updateStatus(@RequestBody UpdateStatusDTO dto){
        return userService.updateStatus(dto);
    }
}

